// adapter/persistence/BookRepositoryAdapter.java
package com.library.bookservice.adapter.persistence;

import com.library.bookservice.domain.model.Book;
import com.library.bookservice.domain.port.BookRepositoryPort;
import com.library.bookservice.domain.exception.BookNotFoundException;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 图书仓库适配器
 * 实现BookRepositoryPort接口，连接JPA仓库
 */
@Component
public class BookRepositoryAdapter implements BookRepositoryPort {

    private final BookJpaRepository bookJpaRepository;

    public BookRepositoryAdapter(BookJpaRepository bookJpaRepository) {
        this.bookJpaRepository = bookJpaRepository;
    }

    @Override
    public Book save(Book book) {
        BookEntity entity = toEntity(book);
        BookEntity savedEntity = bookJpaRepository.save(entity);
        return toDomain(savedEntity);
    }

    @Override
    public Book findById(Long id) throws BookNotFoundException {
        return bookJpaRepository.findById(id)
                .map(this::toDomain)
                .orElseThrow(() -> new BookNotFoundException("ID为 " + id + " 的图书不存在"));
    }

    @Override
    public Optional<Book> findByIsbn(String isbn) {
        return bookJpaRepository.findByIsbn(isbn)
                .map(this::toDomain);
    }

    @Override
    public List<Book> findAll() {
        return bookJpaRepository.findAll().stream()
                .map(this::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public List<Book> findByAuthor(String author) {
        return bookJpaRepository.findByAuthor(author).stream()
                .map(this::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public List<Book> findByGenre(String genre) {
        return bookJpaRepository.findByGenre(genre).stream()
                .map(this::toDomain)
                .collect(Collectors.toList());
    }

    @Override
    public boolean existsByIsbn(String isbn) {
        return bookJpaRepository.existsByIsbn(isbn);
    }

    @Override
    public void deleteById(Long id) {
        bookJpaRepository.deleteById(id);
    }

    /**
     * 将领域对象转换为JPA实体
     */
    private BookEntity toEntity(Book book) {
        if (book.getId() != null) {
            BookEntity entity = new BookEntity();
            entity.setId(book.getId());
            entity.setIsbn(book.getIsbn());
            entity.setTitle(book.getTitle());
            entity.setAuthor(book.getAuthor());
            entity.setGenre(book.getGenre());
            entity.setPublicationDate(book.getPublicationDate());
            entity.setAvailable(book.isAvailable());
            entity.setTotalCopies(book.getTotalCopies());
            entity.setAvailableCopies(book.getAvailableCopies());
            return entity;
        }
        return new BookEntity(
                book.getIsbn(),
                book.getTitle(),
                book.getAuthor(),
                book.getGenre(),
                book.getPublicationDate(),
                book.getTotalCopies()
        );
    }

    /**
     * 将JPA实体转换为领域对象
     */
    private Book toDomain(BookEntity entity) {
        Book book = new Book(
                entity.getIsbn(),
                entity.getTitle(),
                entity.getAuthor(),
                entity.getGenre(),
                entity.getPublicationDate(),
                entity.getTotalCopies()
        );
        book.setId(entity.getId());
        book.setAvailable(entity.isAvailable());
        book.setAvailableCopies(entity.getAvailableCopies());
        return book;
    }
}